package com.csbr.prod.Controller;

import com.csbr.prod.Dao.UsersDao;
import com.csbr.prod.Pojo.UsersEntity;
import com.csbr.prod.Utils.SendMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UsersController {


    @Autowired
    private UsersDao usersDao;

    @RequestMapping("/user")
    public SendMessage fun(@RequestBody UsersEntity jsons){
        Object aa=usersDao.findByUsernameAndPassword(jsons.getUsername(),jsons.getPassword());
        if(aa==null){
            return SendMessage.Fail("error");
        }
        return SendMessage.Success(aa);
    }

    @RequestMapping("/Alluser")
    public SendMessage AllUsers(){
        return SendMessage.Success(usersDao.findAll());
    }

    @RequestMapping("/UpdateUsers")
    public SendMessage UpdateUsers(@RequestBody UsersEntity usersjsons){
        System.out.println(usersjsons);
        usersDao.save(usersjsons);
        return SendMessage.Success(usersDao.findAll());
    }

    @RequestMapping("/DeleteUsers/{userId}")
    public void DeleteUsers(@PathVariable Integer userId){
        usersDao.delete(userId);
    }

    @RequestMapping("/SearchUsers")
    public SendMessage SearchUsers(@RequestBody UsersEntity usersjsons){
        System.out.println(usersDao.findByUsernameLike('%'+usersjsons.getUsername()+'%'));
        // String name =java.net.URLDecoder.decode(username)
        // System.out.println(usersDao.findlike(username).toString());
        return SendMessage.Success(usersDao.findByUsernameLike('%'+usersjsons.getUsername()+'%'));
    }
  
}
